<template>
	<view>
		<cu-custom :isBack="true">
			<block slot="backText"></block>
		</cu-custom>
		<form>
			<view class="cu-form-group">
				<view class="title">{{$t('type')}}</view>
				<picker @change="typeChange" :value="typeIndex" :range="typeNameList">
					<view class="picker">
						{{typeNameList[typeIndex]}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">{{$t('user')}}</view>
				<picker @change="userChange" :value="userIndex" :range="userNameList">
					<view class="picker">
						{{userNameList[userIndex]}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group" v-for="(describe,describeIndex) in workOrder.describeList" :key="describeIndex">
				<view class="title">{{describe.name}}</view>
				<input v-model="describe.value" />
			</view>
			<view class="cu-form-group align-start margin-top">
				<view class="title">{{$t('problem')}}</view>
				<textarea maxlength="-1" v-model="workOrder.problem"></textarea>
			</view>
			<view class="cu-bar bg-white margin-top">
				<view class="action">
					{{$t('enclosure')}}
				</view>
				<view class="action">
					{{workOrder.enclosureList.length}}/4
				</view>
			</view>
			<view class="cu-form-group">
				<view class="grid col-4 grid-square flex-sub">
					<view class="bg-img" v-for="(item,index) in workOrder.enclosureList" :key="index" @tap="ViewImage"
						:data-url="workOrder.enclosureList[index].downloadPath">
						<image :src="workOrder.enclosureList[index].downloadPath" mode="aspectFill"></image>
						<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
							<text class='cuIcon-close'></text>
						</view>
					</view>
					<view class="solids" @tap="ChooseImage" v-if="workOrder.enclosureList.length<4">
						<text class='cuIcon-cameraadd'></text>
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white solid-bottom margin-top">
				<view>
				</view>
				<button class="cu-btn shadow text-white margin-right"
					:style="{backgroundColor:themeConfig.primaryColor}" @tap="saveWorkOrder"
					data-target="menuModal">{{$t('save')}}</button>

			</view>
		</form>
	</view>
</template>

<script>
	import myConfig from '@/myConfig.js'
	import * as erpWorkOrderApi from '@/api/erp/erpWorkOrderApi.js'
	import * as erpWorkOrderTypeApi from '@/api/erp/erpWorkOrderTypeApi.js'
	import * as erpWorkOrderTypeDescribeKeyApi from '@/api/erp/erpWorkOrderTypeDescribeKeyApi.js'
	import * as erpWorkOrderTypeUserApi from '@/api/erp/erpWorkOrderTypeUserApi.js'
	import * as storage from '@/util/storage.js'
	export default {
		data() {
			return {
				typeIndex: 0,
				userIndex: 0,
				userId: storage.getUserId(),
				companyId: storage.getCompanyId(),
				workOrder: {
					describeList: [],
					enclosureList: [],
				},
				typeList: [],
				typeNameList: [],
				describeKeyList: [],
				userList: [],
				userNameList: [],
			}
		},
		computed: {
			themeConfig() {
				return this.$store.state.themeConfig
			}
		},
		created() {
			this.workOrder.createdUserId = this.userId
			this.workOrder.companyId = this.companyId
			this.listWorkOrderType()
		},
		methods: {
			typeChange(e) {
				this.typeIndex = e.detail.value
				this.workOrder.typeId = this.typeList[this.typeIndex].typeId
			},
			userChange(e) {
				this.userIndex = e.detail.value
				this.workOrder.handleUserId = this.userList[this.userIndex].userId
			},
			saveWorkOrder() {
				erpWorkOrderApi.insertWorkOrder({
					...this.workOrder
				}).then(res => {
					const {
						data
					} = res
					if (data) {
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							});
						}, 1000);
					}
				})
			},
			listWorkOrderType() {
				erpWorkOrderTypeApi.listWorkOrderType({
					companyId: this.companyId
				}).then(res => {
					const {
						data
					} = res
					if (data) {
						this.typeList = data
						const typeNameList = []
						data.forEach(item => {
							typeNameList.push(item.name)
						})
						this.typeNameList = typeNameList
						this.typeIndex = 0
						this.workOrder.typeId = data[0].typeId
						this.listWorkOrderTypeDescribeKey()
						this.listWorkOrderTypeUser()
					} else {
						this.typeList = []
					}
				})
			},
			listWorkOrderTypeDescribeKey() {
				erpWorkOrderTypeDescribeKeyApi.listWorkOrderTypeDescribeKey({
					typeId: this.workOrder.typeId
				}).then(res => {
					const {
						data
					} = res
					if (data) {
						const describeList = []
						for (const item of data) {
							describeList.push({
								keyId: item.keyId,
								name: item.name,
								value: '',
								required: item.required
							})
						}
						this.workOrder.describeList = describeList
					} else {
						this.workOrder.describeList = []
					}
				})
			},
			listWorkOrderTypeUser() {
				erpWorkOrderTypeUserApi.listWorkOrderTypeUser({
					typeId: this.workOrder.typeId
				}).then(res => {
					const {
						data
					} = res
					if (data) {
						this.userList = data
						const userNameList = []
						data.forEach(item => {
							userNameList.push(item.userName)
						})
						this.userNameList = userNameList
						this.workOrder.handleUserId = data[0].userId
					} else {
						this.userList = []
						this.userNameList = []
						this.workOrder.handleUserId = null
					}
				})
			},
			ChooseImage() {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						const filePaths = res.tempFilePaths
						filePaths.forEach(filePath => {
							uni.uploadFile({
								url: myConfig.app.baseUrl +
									'/erp/work/order/enclosure/uploadAndGet',
								filePath: filePath,
								header: {
									'access-token': storage.getToken()
								},
								name: 'file',
								success: (uploadFileRes) => {
									const res = JSON.parse(uploadFileRes.data)
									const {
										data
									} = res
									if (data) {
										this.workOrder.enclosureList.push({
											enclosureId: data.enclosureId,
											downloadPath: filePath
										})
									}
								},
								fail(e) {
									console.log(e)
								}
							});
						})
					}
				});
			},
			ViewImage(e) {
				const urls = []
				this.workOrder.enclosureList.forEach(item => {
					urls.push(item.downloadPath)
				})
				uni.previewImage({
					urls: urls,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					content: this.$t('delete'),
					cancelText: this.$t('cancel'),
					confirmText: this.$t('confirm'),
					success: res => {
						if (res.confirm) {
							this.workOrder.enclosureList.splice(e.currentTarget.dataset.index, 1)
						}
					}
				})
			},
		}
	}
</script>

<style>

</style>
